import Instance from "./Instance";

/**
 * @param {string} type
 * @param {string} url
 * @param {*} data
 */
export default function ajax (type, url, data) {
  return new Promise((resolve, reject) => {
    let promise;
    switch (type.toLowerCase()) {
      case "get":
        promise = Instance.get(url, {
          params: data
        });
        break;
      case "post":
        promise = Instance.post(url, data);
        break;
      case "put":
        promise = Instance.put(url, data);
        break;
      case "delete":
        promise = Instance.delete(url, data);
        break;
      default:
        return new Error("没有集成其他方法,只有Get ,Post, Put , Delete 可用");
    }
    promise.then(response => {
      resolve(response);
    }).catch(error => {
      alert(`请求出错了${error.message}`);
    })
  })
}
